<?php
$server = new soap_server;
$namespace = 'uri:faq';

$server->configureWSDL('faqServer',$namespace,$location);
$server->wsdl->schemaTargetNamespace = $namespace;


$server->wsdl->addComplexType
(
    'faqGroup',
    'complexType',
    'struct',
    'all',
    '',
    array
    (
        'id' => array('name'=>'id','type'=>'xsd:int'),
        'name' => array('name'=>'name','type'=>'xsd:string'),
        'visible' => array('name'=>'visible','type'=>'xsd:boolean'),
		'author' => array('name'=>'author','type'=>'xsd:string'),
		'date' => array('name'=>'date','type'=>'xsd:string'),
    )
);

$server->wsdl->addComplexType
(
    'faqGroupArray',
    'complexType',
    'array',
    '',
    'SOAP-ENC:Array',
    array(),
    array(
        array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:faqGroup[]')
    ),
    'tns:faqGroup'
);

$server->wsdl->addComplexType
(
    'faqItem',
    'complexType',
    'struct',
    'all',
    '',
    array
    (
        'id' => array('name'=>'id','type'=>'xsd:int'),
        'author' => array('name'=>'author','type'=>'xsd:string'),
		'subject' => array('name'=>'subject','type'=>'xsd:string'),
		'mail' => array('name'=>'mail','type'=>'xsd:string'),
		'homepage' => array('name'=>'homepage','type'=>'xsd:string'),
		'query' => array('name'=>'query','type'=>'xsd:string'),
		'answer' => array('name'=>'answer','type'=>'xsd:string'),
		'answer_author' => array('name'=>'answer_author','type'=>'xsd:string'),
		'query_date' => array('name'=>'query_date','type'=>'xsd:string'),
		'answer_date' => array('name'=>'answer_date','type'=>'xsd:string'),
		'visible' => array('name'=>'visible','type'=>'xsd:boolean'),
    )
);

$server->wsdl->addComplexType
(
    'faqItemArray',
    'complexType',
    'array',
    '',
    'SOAP-ENC:Array',
    array(),
    array(
        array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:faqItem[]')
    ),
    'tns:faqItem'
);


$server->register('getGroups',array(),array('ret' => 'tns:faqGroupArray'),$namespace,$namespace . '/getGroups','rpc','encoded');
$server->register('getItems',array('group_id' => 'xsd:int'),array('ret' => 'tns:faqItemArray'),$namespace,$namespace . '/getItems','rpc','encoded');
$server->register('getItem',array('id' => 'xsd:int'),array('ret' => 'tns:faqItem'),$namespace,$namespace . '/getItem','rpc','encoded');


function getGroups()
{
	global $bd;
	$sql = 'SELECT * FROM ' . TABLE_PREFIX . 'faq_groups';
	if (!$_SESSION['login'])
	{
		$sql .= " WHERE visible=1";
	}
	$bd->QueryObj($sql);
	if (!$bd->data)
	{
		return new soap_fault('FAQ', '', 'No FAQs');
	}
	$ret = array();
	for(;$bd->data;$bd->NextObj())
	{
		$ret[] = array('id' => $bd->data->id, 'name' => $bd->data->name, 'visible' => $bd->data->visible, 'date' => $bd->data->date, 'author' => $bd->data->author);
	}
	return $ret;
}

function getItems($id)
{
	$id = intval ($id);
	global $bd;

	$sql = 'SELECT * FROM ' . TABLE_PREFIX . 'faq_items WHERE group_id = "' . $id . '"';
	if (!$_SESSION['login'])
	{
		$sql .= ' AND visible=1 AND answer != ""';
	}

	$ret = array();

	for($bd->QueryObj($sql);$bd->data;$bd->NextObj())
	{
		$ret[] = array(
			'id' => $bd->data->id,
			'author' => $bd->data->author,
			'subject' => $bd->data->subject,
			'mail' => $bd->data->mail,
			'homepage' => $bd->data->homepage,
			'query' => $bd->data->query,
			'answer' => $bd->data->answer,
			'answer_author' => $bd->data->answer_author,
			'query_date' => $bd->data->query_date,
			'answer_date' => $bd->data->answer_date,
			'visible' => $bd->data->visible,
		);
	}
	return $ret;
}




if (substr_count($_SERVER['REQUEST_URI'],'wsdl')) //hack ... but it`s work
{
	header("Content-Type: text/xml; charset=ISO-8859-1\r\n");
	die($server->wsdl->serialize(false));
}


$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA); 
?>